import MsuiFormItemModel from './msui-form-item-model'

class MsuiFormModel {
  constructor(formModel) {
    this.init(formModel)
  }
  init(formModel) {
    for (let key in formModel) {
      this[key] = new MsuiFormItemModel({
        value: '',
        label: formModel[key]
      })
    }
  }
  addRules(rules) {
    for (let item in this) {
      this[item].addRules(rules)
    }
  }
  setData(data) {
    for (let key in data) {
      if (this[key]) {
        this[key].setValue(data[key])
      }
    }
  }
  setShow(arr, boolean) {
    if (arr) {
      arr.forEach((item) => {
        this[item].setShow(boolean)
      })
    } else {
      for (let key in this) {
        this[key].setShow(boolean)
      }
    }
  }
  getData() {
    let res = {}
    for (let key in this) {
      res[key] = this[key].getValue()
    }
    return res
  }
  batch({ witch, execute, components, option }) {
    witch.forEach((item) => {
      if (execute == 'type') {
        item[execute](option, components)
      } else {
        item[execute](option)
      }
    })
    return this
  }
}

export default MsuiFormModel
